package com.csdj.service.impl;

import com.csdj.dao.AccountMapper;
import com.csdj.dao.TransactionRecordMapper;
import com.csdj.entity.Account;
import com.csdj.entity.Page;
import com.csdj.entity.TransactionRecord;
import com.csdj.exception.BussinessException;
import com.csdj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private TransactionRecordMapper transMapper;

    @Override
    public boolean login(String cardNo, String passWord) {
        int count = accountMapper.login(cardNo, passWord);
        if(count==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public double seeBanlance(String cardNo) {
        return accountMapper.seeBanlance(cardNo);
    }

    /**
     * 根据卡号获取账户信息
     * @param cardNo
     * @return
     */
    @Override
    public Account getAccount(String cardNo) {
        return accountMapper.getAccount(cardNo);
    }

    //本账户   转出  取款     余额减少   修改
    //转入账户   转入   存款    余额增加   修改
    //两条转账记录    新增转账记录
    // 转入  存款记录
    //本账户  取款记录

    /**
     * 转账
     * @param cardNo 本账户
     * @param toCardNo 目标账户
     * @param money 交易金额
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    public boolean transfer(String cardNo,String toCardNo,double money) throws BussinessException {
        /*
          转账业务过程  1.本账户转出money元，更新本账户余额，2.本账户产生取款交易记录
                      3.目标账户转入money元，更新目标账户余额  4.目标账户产生存款交易记录
         */
        accountMapper.quMoney(money,cardNo);//转出money元
        double cardNoBalance = accountMapper.seeBanlance(cardNo);//转出后本账户的余额

        //本账户交易记录   取款
        transMapper.transfer(new TransactionRecord(cardNo,new Date(),money,cardNoBalance,"取款","赔钱货"));

        accountMapper.cunMoney(money,toCardNo);//目标账户转入money元
        double toBalance = accountMapper.seeBanlance(toCardNo);//转入目标账户的余额
        transMapper.transfer(new TransactionRecord(toCardNo,new Date(),money,toBalance,"存款","谢谢老板"));
        return true;
    }


    @Override
    public Page findRecord(String starDate, String endDate, int curPageNo,int pageSize) {

        Page page = new Page();

        page.setCurPageNo(curPageNo);
        page.setPageSize(pageSize);
        //总记录数
        int total = transMapper.count(starDate,endDate);
        //起始位置   （curpageNO-1)*pageSize
        int index = (curPageNo-1)*pageSize;
        List<TransactionRecord> recordList = transMapper.findRecord(index, pageSize, starDate, endDate);
        page.setTotalCount(total);
        page.setPageList(recordList);
        return page;
    }
}
